package de.fhffm.scs.simulator.gui.composites;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;

import de.fhffm.scs.simulator.driver.IndicatorDriver;

public class UserInterface extends Composite implements IndicatorDriver {

    private static Color RED = new Color(Display.getCurrent(), 255, 0, 0);
    private static Color GREEN = new Color(Display.getCurrent(), 0, 255, 0);

    private Label led1;
    private Label led2;
    private Label led3;

    public UserInterface(Composite parent, int style) {
        super(parent, style);
        setLayout(new GridLayout(1, false));

        Group grpUserInterface = new Group(this, SWT.NONE);
        grpUserInterface.setLayout(new GridLayout(3, false));
        grpUserInterface.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
            true, 1, 1));

        grpUserInterface.setText("User Interface");

        Composite composite = new Composite(grpUserInterface, SWT.BORDER);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
            1, 1));
        composite.setLayout(new GridLayout(1, false));

        led1 = new Label(composite, SWT.BORDER | SWT.CENTER);
        led1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        led1.setText("SW");
        led1.setBackground(GREEN);

        Composite composite_1 = new Composite(grpUserInterface, SWT.BORDER);
        composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
            false, 1, 1));
        composite_1.setLayout(new GridLayout(1, false));

        led2 = new Label(composite_1, SWT.BORDER | SWT.CENTER);
        led2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        led2.setText("HW");
        led2.setBackground(GREEN);

        Composite composite_2 = new Composite(grpUserInterface, SWT.BORDER);
        composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
            false, 1, 1));
        composite_2.setLayout(new GridLayout(1, false));

        led3 = new Label(composite_2, SWT.BORDER | SWT.SHADOW_IN
            | SWT.CENTER);
        led3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        led3.setText("BSL");
        led3.setBackground(GREEN);

        Composite haptics = new Composite(grpUserInterface, SWT.BORDER);
        haptics.setLayout(new GridLayout(2, false));
        haptics.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

        Label lblNewLabel = new Label(haptics, SWT.NONE);
        lblNewLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
        lblNewLabel.setImage(SWTResourceManager.getImage("res/sound.gif"));
        lblNewLabel.setBounds(0, 0, 64, 64);

        Label lblVibration = new Label(haptics, SWT.NONE);
        lblVibration.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
        lblVibration.setImage(SWTResourceManager.getImage("res/vibration.png"));
        lblVibration.setBounds(0, 0, 64, 64);
    }

    @Override
    public void indicateSoftwareError() {
        led1.setBackground(RED);
    }

    @Override
    public void indicatorHardwareError() {
        led2.setBackground(RED);
    }

    @Override
    public void indicateBSLCritical() {
        setBackgroundAsync(led3, RED);
    }

    @Override
    public void indicateBSLOK() {
        setBackgroundAsync(led3, GREEN);
    }

    private void setBackgroundAsync(final Label label, final Color color) {
        Display.getDefault().asyncExec(new Runnable() {

            @Override
            public void run() {
                label.setBackground(color);
            }
        });
    }

}
